require.config({
    baseUrl: 'js/common',
    paths: {
        "deptData": "../data/deptData",
        "zhuguanData": "../data/zhuguanData",
        "pagin": "./pagin",
        "monthData": '../data/monthData',
        "buzhuData": '../data/buzhuData',
        "common": "./common"
    }
    // baseUrl: 'js/common'
});

require(['pagin', 'deptData', 'buzhuData', 'monthData', 'common'], function(pagin, D, BZ, M, Common) {
    // var baseUrl = defaultStr();
    /*----------
     *ajax数据请求
     *部门页面管理————补助页面
     *部门页面管理————补发页面
     *默认打开该页的时候就已经知道了是那个部门
     *并且只显示该部门的信息
     */

    var shenhe_table = new Vue({
        el: '#main-data',
        data: {
            items: [],
            check_Allmonth: '',
            status_action: [{
                statusCode: 1,
                statusName: '已提交待审核(正常补助)',
                type: 0
            }, {
                statusCode: 1,
                statusName: '已提交待审核（补发补助）',
                type: 1
            }],
            shenhe_status: [{
                statusCode: 2,
                statusName: '审核通过'
            }, {
                statusCode: 3,
                statusName: '审核未通过'
            }],
            query: {
                "currentPage": 0,
                "pageSize": 8,
                "statusCode": 1, //0:未提交 1:已提交待审核 2:审核通过 3:审核未通过
                "helpDate": '',
                "deptId": '',
                "type": '' //0：正常发放 1：漏发
            },
            dates: '',
            check_deptId: [],
            check_month: [],
            check_idAll: false,
            check_Allmonth: false,
            check_AllDept: false,
            check_id: [],
            depts: [],
            /*pages:用于构造分页的数组*/
            pages: [],
            totalPage: 1,
            /*cur_page 当前页*/
            curPage: 1
        },
        methods: {
            /*初始化页面*/
            init: function() {
                var _self = this;
                _self.queryData();
                _self.queryMonth();
                _self.queryDepts();
            },
            /*对部门的查询*/
            queryDepts: function() {
                var _self = this;
                var data = {
                    "rows": 100,
                    "page": 1,
                };
                D.queryDepts(data).done(function(result) {
                    _self.depts = [];
                    $.each(result.result, function(i, value) {
                        _self.depts.push(value);
                    });
                });
            },
            /*对日期的查询*/
            queryMonth: function() {
                var _self = this;
                _self.dates = [];
                M.getMonth().done(function(result) {
                    $.each(result.body, function(i, value) {
                        if (parseInt(value.helpMonth) >= 10) {
                            var temp = value.helpYear + '' + value.helpMonth
                        } else {
                            var temp = value.helpYear + '0' + value.helpMonth

                        }
                        _self.dates.push(temp);
                    })
                })
            },
            /*用户提交查询信息，查询学生信息*/
            queryData: function(str) {
                var _self = this;
                if (str == "all") {
                    _self.query.currentPage = 0;
                    _self.curPage = 1;
                }
                _self.query.type = _self.query_type;
                _self.query.deptId = Common.arrayToStr(_self.check_deptId);
                _self.query.helpDate = Common.arrayToStr(_self.check_month);
                // _self.query.deptId = "006,005"
                _self.items = [];
                console.log(_self.query);
                /*提交数据到服务器 query是要查询的数据对象*/
                BZ.queryBuZhu(_self.query).done(function(result) {
                    $.each(result.body.result, function(i, val) {
                        _self.items.push(val)
                    });
                    if (result.body.total > _self.query.pageSize) {
                        /*如果总数大于10就分页*/
                        /*_self.totalPage 总页数*/
                        console.log('fenye')
                        _self.totalPage = Math.ceil(result.body.total / _self.query.pageSize);
                        var result = pagin.paginGetli(_self.totalPage, _self.curPage);
                        _self.pages = result.pages;
                    } else {
                        _self.pages = [];
                    }
                })
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                console.log(cur_page);
                _self.query.currentPage = cur_page - 1;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理助管列表分页查询*/
            submitPage: function(p, b) {
                var _self = this;
                console.log(p);
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            submitShenhe: function(str) {
                var _self = this;
                if (str == "agree") {
                    BZ.agree(_self.check_id).done(function(data) {
                        alert(data.message);
                        // location.reload();
                        // $('#')
                        window.location.reload();
                    })
                } else {
                    BZ.disagree(_self.check_id).done(function(data) {
                        alert(data.message);
                        // _self.queryData('all');
                        window.location.reload();

                    })
                }
            },
            checkAllMonth: function() {
                var _self = this;
                _self.check_month = [];
                if (!_self.check_Allmonth) {
                    $.each(_self.dates, function(i, value) {
                        console.log(value);
                        _self.check_month.push(value);
                    })
                };
                // _self.queryData('all');
            },
            checkAllDept: function() {
                var _self = this;
                console.log('check_Allmonth');
                _self.check_deptId = [];
                if (!_self.check_AllDept) {
                    $.each(_self.depts, function(i, value) {
                        _self.check_deptId.push(value.deptId);
                    })
                };
            },
            checkIdAll: function() {
                var _self = this
                _self.check_id = []
                if (!_self.check_idAll) {
                    $.each(_self.items, function(i, val) {
                        _self.check_id.push(val.id)
                    });
                } else {
                    _self.check_id = []
                }
            }

        },
        watch: {
            check_deptId: function(val) {
                console.log(this.check_deptId);
                if (val.length < this.depts.length) {
                    this.check_AllDept = false;
                } else {
                    this.check_AllDept = true;

                }
                this.queryData('all');
            },
            check_month: function(val) {
                console.log(this.check_month);
                if (val.length < this.dates.length) {
                    this.check_Allmonth = false;
                } else {
                    this.check_Allmonth = true;

                }
                this.queryData('all');
            },
            query_type: function(val) {
                console.log(this.check_month);
                this.queryData('all');
            },
            check_id: function(val) {
                if (val.length < this.items.length) {
                    this.check_idAll = false;
                } else {
                    this.check_idAll = true;

                }
            }
        }

    });
    shenhe_table.init();
});